package leetcode;

public class Test0162 {
    public int findPeakElement(int[] nums) {
        if(nums.length==1)
            return 0;
        int start=0,end=nums.length-1;
        while (start<=end){
            int mid=(end+start)/2;
            if(mid>0 && mid<nums.length-1
                    && nums[mid]>nums[mid+1] && nums[mid]>nums[mid-1])
                return mid;
            else if(mid==0 && nums[mid]>nums[mid+1]){
                return mid;
            }
            else if(mid==nums.length-1 && nums[mid]>nums[mid-1]){
                return mid;
            }
            else if(mid<nums.length-1 && nums[mid]<nums[mid+1]){//右侧更高
                start=mid+1;
            }
            else if(mid>0 && nums[mid-1]>nums[mid]) {//左侧更高
                end=mid-1;
            }
        }
        return start;
    }
}
